<?php
/******************************************
 * Author: <YOUR NAME HERE>
 * Description: <YOUR DESCRIPTION HERE>
 ******************************************/
class Tsmetki extends CustomSecurity { 


	function Tsmetki($db){ 
    }

	/***********************************
 	 * Add:         Allows the insertion of values into the Table.
 	 * Parameters:  $id, $date, $s_descr, $sum, $comment, $categ
 	 * Return:      (Boolean) True - Successfully Inserted | False - Error
 	 ************************************/
	function add($date, $s_descr, $sum, $comment, $categ, $type){ 

		escape_string($date);
		escape_string($s_descr);
		escape_string($sum);
		escape_string($comment);
		escape_string($categ);
		escape_string($type);
		
		$groupId = $_SESSION['group_id'];
		if (strcasecmp($groupId, '-1') == 0){
			$groupId = null;
		}

		$statement = "INSERT INTO tsmetki (date, s_descr, sum, comment, categ, vid, userid, groupId) VALUES ('$date', '$s_descr', '".str_replace(",", ".", $sum)."', '$comment', '$categ', '$type', '".$_SESSION['user_id']."', '".$groupId."');";
		$results   = mysql_query($statement);

		if($results){
			return true;
		}else{
			return false;
		}

	}//End add()


	/***********************************
 	 * Remove:      Allows for the removal of record from the database.
 	 * Parameters:  (String)Primary Key Column Name | (Int) Primary Key Value
 	 * Return:      (Boolean) True - Successfully Removed | False - Error
 	 ************************************/
	function remove($primaryKeyValue){ 
		
		escape_string($primaryKeyValue);

		$statement = "DELETE FROM tsmetki WHERE id = '$primaryKeyValue'";
		$results   = mysql_query($statement);
		if($results){
			return true;
		}else{
			return false;
		}

	}//End remove()


	/***********************************
 	 * Update:      Allows for the update of a record from the database.
 	 * Parameters:  (String)Primary Key Column Name | (Int) Primary Key Value | (String) Column Name To Update | (String) New Value
 	 * Return:      (Boolean) True - Successfully Updated | False - Error
 	 ************************************/
	function update($primaryKeyValue, $columnNameToUpdate, $columnValue){ 

		escape_string($primaryKeyValue);
		escape_string($columnNameToUpdate);
		escape_string($columnValue);
		
		$statement = "UPDATE tsmetki SET $columnNameToUpdate = '$columnValue' WHERE id = '$primaryKeyValue' AND (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].")";
		$results   = mysql_query($statement);
		if($results){
			return true;
		}else{
			return false;
		}

	}//End update()


	/***********************************
 	 * GetAll:       Returns all the records in the database.
 	 * Parameters:   NA
 	 * Return:      (MultiDimensional-Array)String
 	 ************************************/
	function getAll(){ 

		$statement = "SELECT t.id, date, s_descr, sum, comment, category, vid FROM tsmetki t INNER JOIN categs_smetki c ON categ = c.id WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].") ORDER BY date DESC";
		$results   = mysql_query($statement);
		
		
		$tokens[0] = 'id';
		$tokens[1] = 'date';
		$tokens[2] = 's_descr';
		$tokens[3] = 'sum';
		$tokens[4] = 'comment';
		$tokens[5] = 'category';
		$tokens[6] = 'vid';
		
		return transformResults($results, $tokens);

	}//End getAll()
	
	function getEntries(){ 

		$statement = "SELECT DISTINCT date_format(date, '%M %Y'), DATE_FORMAT( date, CONCAT(ELT( MONTH(date), 'Януари','Февруари','Март','Април','Май','Юни','Юли','Август','Септември','Октомври','Ноември','Декември'),' %Y')) FROM tsmetki WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].") ORDER BY date DESC";
		$results   = mysql_query($statement);
		
		$tokens[0] = "date_format(date, '%M %Y')";
		return transformResults($results, $tokens);

	}
	
	function getByMonthEntries($filter){ 

		escape_string($filter);

		$statement = "SELECT t.id, date, s_descr, sum, comment, category, vid FROM tsmetki t INNER JOIN categs_smetki c ON categ = c.id WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].") AND date_format(date, '%M %Y')='$filter' ORDER BY date DESC";
		$results   = mysql_query($statement);
		
		$tokens[0] = 't.id';
		$tokens[1] = 'date';
		$tokens[2] = 's_descr';
		$tokens[3] = 'sum';
		$tokens[4] = 'comment';
		$tokens[5] = 'category';
		$tokens[6] = 'vid';
		
		return transformResults($results, $tokens);

	}
	
	


	/***********************************
 	 * GetObject:   Returns a specific record form the batabase.
 	 * Parameters:  (Int) Primary Key Value
 	 * Return:      (Array)String
 	 ************************************/
	function getObject($primaryKeyValue){ 

		escape_string($primaryKeyValue);

		$statement = "SELECT id, date, s_descr, sum, comment, categ, vid FROM tsmetki WHERE id = '".$primaryKeyValue."'";
		$results   = mysql_query($statement);
		$tokens[0] = 'id';
		$tokens[1] = 'date';
		$tokens[2] = 's_desct';
		$tokens[3] = 'sum';
		$tokens[4] = 'comment';
		$tokens[5] = 'categ';
		$tokens[6] = 'vid';
		
		return transformResults($results, $tokens);

	}//End getObject()

		/***********************************
 	 * GetObject:   Returns a specific record form the batabase.
 	 * Parameters:  (Int) Primary Key Value
 	 * Return:      (Array)String
 	 ************************************/
	function getSmetkiLongDescr($sub){ 
		
		escape_string($sub);

		$statement = "SELECT DISTINCT comment FROM tsmetki WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].") AND comment LIKE LOWER('%$sub%')";
		$results   = mysql_query($statement);
		
		$tokens[0] = 'comment';
		
		return transformResults($results, $tokens);

	}//End getObject()
	
	
		/***********************************
 	 * GetObject:   Returns a specific record form the batabase.
 	 * Parameters:  (Int) Primary Key Value
 	 * Return:      (Array)String
 	 ************************************/
	function getSmetkiShortDescr($sub){ 
		
		escape_string($sub);

		$statement = "SELECT DISTINCT s_descr FROM tsmetki WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].") AND s_descr LIKE LOWER('%$sub%')";

		$results   = mysql_query($statement);
		
		$tokens[0] = 's_descr';
		
		return transformResults($results, $tokens);

	}//End getObject()
	
	function getSmetkiMap(){ 
		

		$statement = "SELECT DISTINCT s_descr, categ FROM tsmetki WHERE (userId=".$_SESSION['user_id']." OR groupId=".$_SESSION['group_id'].")";
		$results   = mysql_query($statement);
		
		$tokens[0] = 's_descr';
		$tokens[1] = 'categ';
		
		return transformResults($results, $tokens);

	}
	
	function countEntries(){
		$statement = "SELECT id  FROM tsmetki";
		$results   = mysql_query($statement);
		return mysql_num_rows($results);
	}

}//End Class Tsmetki
?>